#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>

size_t my_strlen(const char* str)
{
	size_t len = 0;
	while (*str++)
	{
		len++;
	}
	return len;
}

char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int my_strcmp(const char* p1,const char* p2)
{
	while (*p1 == *p2)
	{
		if (*p1 == '\0')
		{
			return 0;
		}
		p1++, p2++;
	}
	return *p1 - *p2;
}

char* my_strcat(char* dest,const char* src)
{
	char* cur = dest;
	while (*cur)
	{
		cur++;
	}
	while (*cur++ = *src++)
	{
		;
	}
	return dest;
}

char* my_strstr(const char* p1,const char* p2)
{
	while (*p1)
	{
		if (*p1 == *p2)
		{
			const char* cur1 = p1, * cur2 = p2;
			while (*cur1 == *cur2 || *cur2 == '\0')
			{
				if (*cur2 == '\0')
				{
					return p1;
				}
				cur1++, cur2++;
			}
		}
		p1++;
	}
	return NULL;

	//char* s = p1 - 1; char* p = p2 - 1;
	//int* ne = (int*)malloc(strlen(p2) * 4);
	//ne[1] = 0;
	//for (int i = 2, j = 0; p[i]; i++)
	//{
	//	while (j && p[j + 1] != p[i])
	//	{
	//		j = ne[j];
	//	}
	//	if (p[i] == p[j + 1])
	//	{
	//		j++;
	//	}
	//	ne[i] = j;
	//}

	//for (int i = 1, j = 0; s[i]; i++)
	//{
	//	while (j && p[j + 1] != s[i])
	//	{
	//		j = ne[j];
	//	}
	//	if (p[j + 1] == s[i])
	//	{
	//		j++;
	//	}
	//	if (j == strlen(p2))
	//	{
	//		return p1 + i - strlen(p2);
	//	}
	//}
	//return NULL;
}

void* my_memcpy(void* dest ,void* src, size_t size)
{
	void* ret = dest;
	while (size--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

void* my_memmove(void* dest,void* src,size_t size)
{
	assert(dest && src);
	void* ret = dest;
	if (dest <= src)
	{
		while (size--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (size--)
		{
			*((char*)dest + size) = *((char*)src + size);
		}
	}
	return ret;
}


int main()
{
	char a[10] = "abcdafghi";
	char b[] = "afgh";
	printf("%s",my_strstr(a, b));
	memset
	return 0;
}

